<?php 
	include_once("connect.php");
	function isLogged()		{
		return isset($_SESSION["user"]);
	}
	function login($login, $password) {
		$found = false;
				
		$result=mysql_query ("select pseudo,password from AP3_SP_user ");
		while ($row=mysql_fetch_assoc($result)) {	
			if($row["pseudo"]==$login && $row["password"]==md5($password))
				$found=true;
		
			if($found){ 
				$_SESSION["user"] = $login;
				$_SESSION["info"] = "";
			}	
			
			else 
				$_SESSION["error"] = "";
		}
	}
	
	function logout()	{
		unset($_SESSION["user"]);
		$_SESSION["info"] = "";
	}
	
	if (isLogged()) { 
		echo '<form action="." method="post">';
		echo '<input type="submit" value="Deconnexion" name="logout"/>';
		echo '</form>';	
	}
	else {
		echo '<form action="." method="post">';
		echo '<input name="login"/>';
		echo '<input name="password" type="password"/>';
		echo '<input type="submit" value="Connexion" name="connec"/>';
		echo '<input type="submit" value="S\'inscrire" name="inscription"/>';
		echo '</form>';
	} 
	
	if(isset($_POST["inscription"]))
		header("location: inscription.php");
		
	echo '<div id="message">';

	if(isset($_SESSION["info"])){
		echo '<div id="infomessage">' .$_SESSION["info"] .'</div>';
		unset($_SESSION["info"]);
	}
	
	if(isset($_SESSION["error"])) {
		echo '<div id="errormessage">' .$_SESSION["error"] .'</div>';
		unset($_SESSION["error"]);
	}
	echo '</div>';
	
	if(isset($_POST["connec"])){
		login($_POST["login"], $_POST["password"]);
		header("location: .");
		exit;
	}

	if (isset($_POST["logout"]))	{
		logout();
		header("location: .");
		exit;
	}				

?>